// Copyright 2013 Google Inc. All Rights Reserved.
//
// Unfortunately, nacl-glibc's clock_getres.c is heavily depending on
// other code in glibc. So, we track nothing for this file.
//

#include <errno.h>
#include <time.h>

#include <irt_syscalls.h>
#include <nacl_timespec.h>

int clock_getres(clockid_t clk_id, struct timespec *res) {
  ENOSYS_IF_NULL(__nacl_irt_clock_getres)

  switch (clk_id) {
    case CLOCK_MONOTONIC:
    case CLOCK_PROCESS_CPUTIME_ID:
    case CLOCK_REALTIME:
    case CLOCK_THREAD_CPUTIME_ID: {
      struct nacl_abi_timespec nacl_res;
      int result = __nacl_irt_clock_getres(clk_id, (struct timespec*) &nacl_res);
      if (result != 0) {
        errno = __nacl_abi_errno_to_errno(result);
        return -1;
      }
      // The manual says res==NULL is OK.
      if (res)
        __nacl_abi_timespec_to_timespec(&nacl_res, res);
      return 0;
    }
    default:
      errno = EINVAL;
      return -1;
  }
}
